package com.qunar.mina.app.config;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DirectoryConfig {
    private static final Logger logger = LoggerFactory.getLogger(DirectoryConfig.class);
    private static Directory fsAddressDirectory;
    private static Directory fsUserDirectory;
    private static Directory fsAuditorDirectory;
    private static Directory fsPicDirectory;
    private static Directory fsRecDirectory;
    private static Directory fsAlbumDirectory;

    private static String address;
    private static String user;
    private static String auditor;
    // private static String pic;
    private static String record;
    private static String album;

    static {
        Properties props = new Properties();
        try {
            props.load(DirectoryConfig.class.getClassLoader().getResourceAsStream("lucene.properties"));
            address = props.getProperty("address");
            user = props.getProperty("user");
            auditor = props.getProperty("auditor");
            // pic = props.getProperty("pic");
            record = props.getProperty("record");
            album = props.getProperty("album");

            fsAddressDirectory = FSDirectory.open(new File(address));
            fsUserDirectory = FSDirectory.open(new File(user));
            fsAuditorDirectory = FSDirectory.open(new File(auditor));
            // fsPicDirectory = FSDirectory.open(new File(pic));
            fsRecDirectory = FSDirectory.open(new File(record));
            fsAlbumDirectory = FSDirectory.open(new File(album));
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }

    }

    public static Directory getFsAddressDirectory() {
        return fsAddressDirectory;
    }

    public static Directory getFSUserDirectory() {
        return fsUserDirectory;
    }

    public static Directory getFSAuditorDirectory() {
        return fsAuditorDirectory;
    }

    public static Directory getFSPicDirectory() {
        return fsPicDirectory;
    }

    public static Directory getFSRecDirectory() {
        return fsRecDirectory;
    }

    public static Directory getFSAlbumDirectory() {
        return fsAlbumDirectory;
    }

    public static Directory getFSDirectory(String dirName) {

        if (dirName == null || "".equals(dirName))
            return null;
        if (dirName.equalsIgnoreCase("address"))
            return fsAddressDirectory;
        if (dirName.equalsIgnoreCase("auditor"))
            return fsAuditorDirectory;
        if (dirName.equalsIgnoreCase("user"))
            return fsUserDirectory;
        if (dirName.equalsIgnoreCase("record"))
            return fsRecDirectory;
        if (dirName.equalsIgnoreCase("album"))
            return fsAlbumDirectory;

        return null;

    }

}
